<?php
// $connect = mysql_connect('mysql17.000webhost.com','a4117838_simple','123simple');
$connect = mysql_connect('localhost','root','');
if (!$connect){
  die('Could not connect: ' . mysql_error($connect));
}
// mysql_select_db('a4117838_simple',$connect);
mysql_select_db('mydatabase',$connect);

@$dataoption  = $_POST['dataoption'];

if($dataoption=='deletecsv')
{
    $csvname = $_POST['csvname'];
    mysql_query('delete from tablelist where tablename = "'.$csvname.'"');
    mysql_query('drop table '.$csvname);
}
else
{
    $tablename = $_GET['productname'];
    $q1        = $_GET['q1'];
    $answer    = $_GET['answer'];
    
    $tablequery = "SELECT id FROM tablelist where tablename='".$tablename."'";
    $tableresult = mysql_query($tablequery);
    while($tablerow = mysql_fetch_array($tableresult, MYSQL_NUM)){
        
        $table_name = $tablerow[0];
    
    // echo($_GET['subansweroption'][0]);
        // echo $table_name;
        mysql_query("insert into question (question_full,box_email_dropdown,tablename) VALUES('$q1','$answer','$table_name')");
        $questionid = mysql_insert_id();
        
        if($answer=='box')
        {
            $msg1               = $_GET['msg1'];
            mysql_query("insert into answer (answer,question_id) VALUES('$msg1','$questionid')");
        }
        else if($answer=='email')
        {
            $emailtext          = $_GET['emailtext'];
            mysql_query("insert into answer (answer,question_id) VALUES('$emailtext','$questionid')");
        }
        else if($answer=='dropdown')
        {
            $subqoptionnum      = count($_GET['subqoption']);
            $subansweroptionnum = count($_GET['subansweroption']);
            //$subansweroption    = $_GET['subansweroption[]'];

            for($i=0;$i < $subqoptionnum;$i++) {
                $subqoption              = $_GET["subqoption"][$i];
                $subansweroption         = $_GET["subansweroption"][$i];
    //            mysql_query('insert into subquestion (question_id,subquestion_full,box_email_nothing) VALUES('.$questionid.','.$_GET['subqoption['.$i.']'].','.$questionid.')');
                $sql= "insert into subquestion (question_id,subquestion_full,box_email_nothing) VALUES('$questionid','$subqoption','$questionid')";
    	
                mysql_query($sql) ;

                $sql2 = "insert into subanswer(sub_question_id,subanswer) VALUES('".mysql_insert_id()."','$subansweroption')";
                
                mysql_query($sql2);
            }
        } 
    }
    header("location: qanda.php");
}
?>
